library(tidyverse)
library(data.table)
library(tidyr)

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_1/")


USAdat <- read.csv("AHK_USAFinal.csv", stringsAsFactors = FALSE)
JPNdat <- read.csv("AHK_JapanFinal.csv", fileEncoding = "latin1", check.names = FALSE, stringsAsFactors = FALSE)
ROKdat <- read.csv("AHK_ROKFinal.csv", fileEncoding = "latin1", check.names = FALSE, stringsAsFactors = FALSE)
ROKdatP <- read.csv("AHK_ROKFinalP.csv", fileEncoding = "latin1", check.names = FALSE, stringsAsFactors = FALSE)


COMdat <-  # This binds the four raw surveys into one combined dataset
  bind_rows(
    `1` = USAdat,
    `2` = JPNdat,
    `3` = ROKdat,
    .id = "Nationality"
  ) %>%
  mutate(
    American = as.numeric(Nationality == 1),
    Japanese = as.numeric(Nationality == 2),
    Korean = as.numeric(Nationality == 3)
  )

COMdat <-  # This takes the treatment display orders and codes each treatment as a binary
  COMdat %>%
  mutate(
    Japan = if_else(Treatment_DO %in% c("Q5|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                         "Q7|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                         "Q9|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                         "Q11|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                         "Q13|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                         "Q15|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q17|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q19|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16"), 0,  1),
    Nuclear = if_else(Treatment_DO %in% c("Q5|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                          "Q9|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                          "Q13|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                          "Q17|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                          "Q21|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                          "Q25|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                          "Q29|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                          "Q33|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16"), 0,  1),
    Tripwire = if_else(Treatment_DO %in% c("Q5|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q7|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q13|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q15|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q21|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q23|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q29|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q31|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16"), 0,  1),
    Retaliation = if_else(Treatment_DO %in% c("Q5|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q7|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q9|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q11|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q21|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q23|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q25|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16",
                                        "Q27|Q6|Q8|Q10|Q12|Q14|Q36|Q34|Q32|Q30|Q28|Q26|Q24|Q22|Q20|Q18|Q16"), 0,  1),
  )

COMdat <- # This combines treatments to create Scenarios 
  COMdat %>% 
  unite('Scenario', Japan:Retaliation, sep="", remove=FALSE)

COMdat <- # This pulls out the most aggressive option
  COMdat %>% 
  mutate(
    ResponseROK = str_sub(Q37,1,1),
    ResponseJPN = str_sub(Q38,1,1)
    )

COMdat$ResponseROK<-as.numeric(COMdat$ResponseROK) # Converts the strings to numbers
COMdat$ResponseJPN<-as.numeric(COMdat$ResponseJPN)


COMdat <- # Creates the outcome variables
  COMdat %>% 
  mutate(
    MaxResponse = coalesce(ResponseROK, ResponseJPN), # Most aggressive action selected by a respondent
    MaxResponse = if_else(MaxResponse == 9, NA_real_, MaxResponse), # Then create binary variables for whether the respondent's most aggressive choice was:
    MilResponse = as.numeric(MaxResponse %in% c(3, 4, 5)),# Military,
    War = as.numeric(MaxResponse %in% c(4, 5)),# War-level,
    NuclearMissile = as.numeric(MaxResponse %in% c(5)), # Nuclear,
    Conventional = as.numeric(MaxResponse %in% c(3, 4)), # Conventional, or
    Nonmilitary = as.numeric(MaxResponse %in% c(0, 1, 2)), # Non-Military
    NuclearRetal = if_else(Q51 == 99, NA_real_, as.numeric(Q51)), #Create variable for support for nuclear retaliation
    SupNucRetal = as.numeric(NuclearRetal %in% c(4, 5)), # Binary outcome for nuclear retaliation support (1 for level 4 or 5) 
    Q49 = as.numeric(Q49),
    Q49 = if_else(Q49 == 99, NA_real_, Q49),
    Q50 = as.numeric(Q50),
    Q50 = if_else(Q50 == 99, NA_real_, Q50),
    Proliferation = if_else(!is.na(Q49), Q49, Q50), # and Nuclear proliferation by the attacked country
    SupportAlly = as.numeric(Support.Ally %in% c(1)),
    PunishRevenge  = as.numeric(Punish.Revenge %in% c(1)),
    EliminateThreat  = as.numeric(Eliminate.Threat %in% c(1)),
    AvoidNuclearUse  = as.numeric(Avoid.Nuclear.Use %in% c(1)),
    AvoidEntanglement  = as.numeric(Avoid.Entanglement %in% c(1))
  )


COMdat <- # Re-coding demographic variables
  COMdat %>% 
  mutate (
    Ideology = factor(Q62),
    Ideology = as.numeric(Ideology),
    Ideology = if_else(Ideology==1, NA_real_, Ideology),  #dropping "Prefer not to answer"
    Ideology = Ideology-1,       #recalibrating to 1-5
    Q54 = factor(Q54),
    Gender = as.numeric(as.character(Q54)),
    Gender = if_else(Gender==2, 0, 1), # Female = 0, Male = 1
    Q55 = factor(Q55),
    Age = Q55,
    Trump = if_else(Q63 == "1" & Nationality == "1", 1, 0), # Trump Supporter (U.S. Sample Only)
    Income = Q58,
    logIncome = log(Q58)
  )

COMdat$Age = as.numeric(as.character(COMdat$Age))
COMdat$AgeGroup = cut(COMdat$Age, right =F, breaks=c(18,25,35,45,55,65,99)) # Creates age groups

# COMdat # Coalesce Free Responses
#   COMdat %>% 
#     mutate(
#       Q39 = ifelse(Q39=="", NA,Q39),
#       Q40 = ifelse(Q40=="", NA,Q40),
#       Q41 = ifelse(Q41=="", NA,Q41),
#       Q42 = ifelse(Q42=="", NA,Q42),
#       Q43 = ifelse(Q43=="", NA,Q43),
#       Q44 = ifelse(Q44=="", NA,Q44),
#       Q45 = ifelse(Q45=="", NA,Q45),
#      FreeResponse = coalesce(Q39, Q40, Q41, Q42, Q43, Q44, Q45)
#     )


CLEANdat <- # Selects columns for export
  COMdat %>% 
  select(
    Nationality,
    ResponseId,
    Scenario,
    Japan,
    Nuclear,
    Tripwire,
    Retaliation,
    ResponseROK,
    ResponseJPN,
    MaxResponse,
    MilResponse,
    War,
    NuclearMissile,
    Conventional,
    Nonmilitary,
    NuclearRetal,
    SupNucRetal,
    Proliferation,
    Age,
    AgeGroup,
    Ideology,
    Gender,
    Trump,
    Income,
    logIncome,
    SupportAlly,
    PunishRevenge,
    EliminateThreat,
    AvoidNuclearUse,
    AvoidEntanglement
  )

write.csv(CLEANdat,"Paper_1_Data_Clean_tidy.csv", row.names = FALSE)


